package com.ss.test09;

/**
 * @Auther: 520ljs
 * @Date: 2022/3/20 - 03 - 20 - 14:40
 */
public class TestDeadLock implements Runnable {
    public int flag = 1;
    static Object o1 = new Object(), o2 = new Object();

    public void run(){
        System.out.println("flag=" + flag);
        //当flag==1锁住o1
        if (flag == 1){
            synchronized (o1){
                try {
                    Thread.sleep(500);
                }catch (Exception e){
                    e.printStackTrace();
                }
                //只要锁住o2就完成
                synchronized (o2){
                    System.out.println("2");
                }
            }
        }
        //如果flag == 0锁住o2
        if (flag == 0){
            synchronized (o2){
                try {
                    Thread.sleep(500);
                } catch (Exception e){
                    e.printStackTrace();
                }
                //只要锁住o1就完成
                synchronized (o1){
                    System.out.println("3");
                }
            }
        }
    }

    public static void main(String[] args){
        //实例2个线程类
        TestDeadLock td1 = new TestDeadLock();
        TestDeadLock td2 = new TestDeadLock();
        td1.flag = 1;
        td2.flag = 0;
        //开启2个线程
        Thread t1 = new Thread(td1);
        Thread t2 = new Thread(td2);
        t1.start();
        t2.start();
    }
}
